﻿using Automated.ActionFolder;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace Automated
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Actions actions = new Actions();
            if (this.textBox1.Text.Length > 0 && this.textBox1.Text != "")
            {
                try
                {
                    actions.Timeout = int.Parse(this.textBox1.Text);
                }catch(Exception)
                {
                    MessageBox.Show("超时时间错误");
                    return;
                }
            }
            else
            {
                actions.Timeout = 0;
            }
            actions.Items = Globalspace.ActionList;
            this.richTextBox1.Text=JsonConvert.SerializeObject(actions);
        }

        private class Actions{
            private int timeout = 0;
            private List<ActionItem> items=new List<ActionItem>();
            public int Timeout { get => timeout; set => timeout = value; }
            public List<ActionItem> Items { get => items; set => items = value; }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.panel2.Controls.Clear();
            this.show_Item(this.comboBox1.Text);
        }

        private void show_Item(String text)
        {
            switch (text)
            {
                default:
                    break;
                case "点击": break;
                case "启动":
                    showLabel = new Label();
                    showLabel.Text = "浏览器路径:";
                    showLabel.AutoSize = true;
                    showLabel.Location = new System.Drawing.Point(3, 9);
                    showLabel.Name = "label3";
                    showLabel.Size = new System.Drawing.Size(71, 12);
                    showLabel.TabIndex = 0;
                    showBox = new TextBox();
                    showBox.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    showBox.Location = new System.Drawing.Point(3, 24);
                    showBox.Name = "textBox1";
                    showBox.Size = new System.Drawing.Size(175, 26);
                    showBox.TabIndex = 1;
                    this.panel2.Controls.Add(showLabel);
                    this.panel2.Controls.Add(showBox);
                    break;
                case "输入":
                    showLabel = new Label();
                    showLabel.Text = "内容:";
                    showLabel.AutoSize = true;
                    showLabel.Location = new System.Drawing.Point(3, 9);
                    showLabel.Name = "label3";
                    showLabel.Size = new System.Drawing.Size(71, 12);
                    showLabel.TabIndex = 0;
                    showBox = new TextBox();
                    showBox.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    showBox.Location = new System.Drawing.Point(3, 24);
                    showBox.Name = "textBox1";
                    showBox.Size = new System.Drawing.Size(175, 26);
                    showBox.TabIndex = 1;
                    this.panel2.Controls.Add(showLabel);
                    this.panel2.Controls.Add(showBox);
                    break;
                case "切换":
                    showLabel = new Label();
                    showLabel.Text = "窗口号:";
                    showLabel.AutoSize = true;
                    showLabel.Location = new System.Drawing.Point(3, 9);
                    showLabel.Name = "label3";
                    showLabel.Size = new System.Drawing.Size(71, 12);
                    showLabel.TabIndex = 0;
                    showBox = new TextBox();
                    showBox.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    showBox.Location = new System.Drawing.Point(3, 24);
                    showBox.Name = "textBox1";
                    showBox.Size = new System.Drawing.Size(175, 26);
                    showBox.TabIndex = 1;
                    this.panel2.Controls.Add(showLabel);
                    this.panel2.Controls.Add(showBox);
                    break;
                case "定位":
                    showLabel = new Label();
                    showLabel.Text = "目标:";
                    showLabel.AutoSize = true;
                    showLabel.Location = new System.Drawing.Point(3, 9);
                    showLabel.Name = "label3";
                    showLabel.Size = new System.Drawing.Size(71, 12);
                    showLabel.TabIndex = 0;
                    showBox = new TextBox();
                    showBox.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    showBox.Location = new System.Drawing.Point(3, 24);
                    showBox.Name = "textBox1";
                    showBox.Size = new System.Drawing.Size(175, 26);
                    showBox.TabIndex = 1;
                    this.panel2.Controls.Add(showLabel);
                    this.panel2.Controls.Add(showBox);
                    break;
                case "关闭":
                    showLabel = new Label();
                    showLabel.Text = "窗口号:";
                    showLabel.AutoSize = true;
                    showLabel.Location = new System.Drawing.Point(3, 9);
                    showLabel.Name = "label3";
                    showLabel.Size = new System.Drawing.Size(71, 12);
                    showLabel.TabIndex = 0;
                    showBox = new TextBox();
                    showBox.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    showBox.Location = new System.Drawing.Point(3, 24);
                    showBox.Name = "textBox1";
                    showBox.Size = new System.Drawing.Size(175, 26);
                    showBox.TabIndex = 1;
                    this.panel2.Controls.Add(showLabel);
                    this.panel2.Controls.Add(showBox);
                    break;
                case "退出":break;
                case "验证":
                    showLabel = new Label();
                    showLabel.Text = "包含:";
                    showLabel.AutoSize = true;
                    showLabel.Location = new System.Drawing.Point(3, 9);
                    showLabel.Name = "label3";
                    showLabel.Size = new System.Drawing.Size(71, 12);
                    showLabel.TabIndex = 0;
                    showBox = new TextBox();
                    showBox.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    showBox.Location = new System.Drawing.Point(3, 24);
                    showBox.Name = "textBox1";
                    showBox.Size = new System.Drawing.Size(175, 26);
                    showBox.TabIndex = 1;
                    this.panel2.Controls.Add(showLabel);
                    this.panel2.Controls.Add(showBox);
                    break;
                case "截图": break;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            String value = this.comboBox1.Text;
            ActionItem action;
            switch (value)
            {
                default:
                    MessageBox.Show("请选择操作?");
                    break;
                case "点击":
                    action = new ActionItem();
                    action.ActionName = "点击";
                    action.ActionValue = "click";
                    action.ActionDetailed = "";
                    Globalspace.ActionList.Add(action);
                    flushListView();
                    break;
                case "启动":
                    if (checkTextBox())
                    {
                        MessageBox.Show("请指定启动浏览器") ;
                        break;
                    }
                    action = new ActionItem();
                    action.ActionName = "启动";
                    action.ActionValue = "start";
                    action.ActionDetailed = this.showBox.Text;
                    Globalspace.ActionList.Add(action);
                    flushListView();
                    break;
                case "输入":
                    action = new ActionItem();
                    action.ActionName = "输入";
                    action.ActionValue = "input";
                    action.ActionDetailed = this.showBox.Text;
                    Globalspace.ActionList.Add(action);
                    flushListView();
                    break;
                case "切换":
                    if (checkTextBox())
                    {
                        MessageBox.Show("请指定切换窗口号");
                        break;
                    }
                    action = new ActionItem();
                    action.ActionName = "切换";
                    action.ActionValue = "change";
                    action.ActionDetailed = this.showBox.Text;
                    Globalspace.ActionList.Add(action);
                    flushListView();
                    break;
                case "定位":
                    if (checkTextBox())
                    {
                        MessageBox.Show("请指定目标");
                        break;
                    }
                    action = new ActionItem();
                    action.ActionName = "定位";
                    action.ActionValue = "position";
                    action.ActionDetailed = this.showBox.Text;
                    Globalspace.ActionList.Add(action);
                    flushListView();
                    break;
                case "关闭":
                    if (checkTextBox())
                    {
                        MessageBox.Show("请指定关闭窗口号");
                        break;
                    }
                    action = new ActionItem();
                    action.ActionName = "关闭";
                    action.ActionValue = "close";
                    action.ActionDetailed = this.showBox.Text;
                    Globalspace.ActionList.Add(action);
                    flushListView();
                    break;
                case "退出":
                    action = new ActionItem();
                    action.ActionName = "退出";
                    action.ActionValue = "exit";
                    action.ActionDetailed = "";
                    Globalspace.ActionList.Add(action);
                    flushListView();
                    break;
                case "验证":
                    if (checkTextBox())
                    {
                        MessageBox.Show("请输入包含元素");
                        break;
                    }
                    action = new ActionItem();
                    action.ActionName = "验证";
                    action.ActionValue = "check";
                    action.ActionDetailed = this.showBox.Text;
                    Globalspace.ActionList.Add(action);
                    flushListView();
                    break;
                case "截图":
                    action = new ActionItem();
                    action.ActionName = "截图";
                    action.ActionValue = "screenshot";
                    action.ActionDetailed = "";
                    Globalspace.ActionList.Add(action);
                    flushListView();
                    break;
            }
        }

        private void flushListView()
        {
            Object selectObj = this.listBox1.SelectedItem;
            this.listBox1.Items.Clear();
            foreach (ActionItem action in Globalspace.ActionList)
            {
                if (action.ActionDetailed.Length > 0 && action.ActionDetailed != null)
                {
                    this.listBox1.Items.Add(action.ActionName+" "+action.ActionDetailed);
                }
                else
                {
                    this.listBox1.Items.Add(action.ActionName);
                }
            }
            this.listBox1.SelectedItem = selectObj;
        }

        private void flushListView(int selectIndex)
        {
            this.listBox1.Items.Clear();
            foreach (ActionItem action in Globalspace.ActionList)
            {
                if (action.ActionDetailed.Length > 0 && action.ActionDetailed != null)
                {
                    this.listBox1.Items.Add(action.ActionName + " " + action.ActionDetailed);
                }
                else
                {
                    this.listBox1.Items.Add(action.ActionName);
                }
            }
            this.listBox1.SelectedIndex = selectIndex;
        }

        private Boolean checkTextBox()
        {
            if (this.showBox.Text.Length > 0 && this.showBox.Text != "")
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItems.Count > 0)
            {
                if (this.listBox1.SelectedIndex == 0)
                {
                    return;
                }
                int selectIndex = this.listBox1.SelectedIndex;
                List<ActionItem> actions1 = new List<ActionItem>();
                for (int i = 0; i < this.listBox1.SelectedIndex-1; i++)
                {
                    actions1.Add(Globalspace.ActionList[i]);
                }
                actions1.Add(Globalspace.ActionList[this.listBox1.SelectedIndex]);
                actions1.Add(Globalspace.ActionList[this.listBox1.SelectedIndex - 1]);
                for (int i = this.listBox1.SelectedIndex +1; i < this.listBox1.Items.Count; i++)
                {
                    actions1.Add(Globalspace.ActionList[i]);
                }
                Globalspace.ActionList.Clear();
                Globalspace.ActionList = actions1;
                flushListView(selectIndex-1);
            }
            else
            {
                MessageBox.Show("请选择操作项");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItems.Count > 0)
            {
                if (this.listBox1.SelectedIndex == this.listBox1.Items.Count-1)
                {
                    return;
                }
                int selectIndex = this.listBox1.SelectedIndex;
                List<ActionItem> actions1 = new List<ActionItem>();
                for (int i = 0; i < this.listBox1.SelectedIndex; i++)
                {
                    actions1.Add(Globalspace.ActionList[i]);
                }
                actions1.Add(Globalspace.ActionList[this.listBox1.SelectedIndex + 1]);
                actions1.Add(Globalspace.ActionList[this.listBox1.SelectedIndex]);
                for (int i = this.listBox1.SelectedIndex +2; i < this.listBox1.Items.Count; i++)
                {
                    actions1.Add(Globalspace.ActionList[i]);
                }
                Globalspace.ActionList.Clear();
                Globalspace.ActionList = actions1;
                flushListView(selectIndex+1);
            }
            else
            {
                MessageBox.Show("请选择操作项");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItems.Count > 0)
            {
                int selectIndex = this.listBox1.SelectedIndex;
                Globalspace.ActionList.RemoveAt(selectIndex);
                flushListView();
            }
            else
            {
                MessageBox.Show("请选择操作项");
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            Globalspace.ActionList.Clear();
            flushListView();
        }
    }
}
